package com.xy.transfer.service.impl;

import com.alibaba.fastjson.JSON;
import com.xy.transfer.dao.TransferDao;
import com.xy.transfer.entity.Transfer;
import com.xy.transfer.feign.BankICBCFeign;
import com.xy.transfer.service.BankICBCService;
import com.xy.transfer.utils.NumberUtil;
import com.xy.transfer.utils.RSAUtils;
import com.xy.transfer.vo.TransferInfoVO;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class BankICBCServiceImpl implements BankICBCService {
    @Autowired
    BankICBCFeign bankICBCFeign;
    @Value("${rsa.public.key}")
    private String publicKey;
    @Autowired
    TransferDao transferDao;

    @GlobalTransactional
    @Transactional
    public String transferAcc(TransferInfoVO vo) {
        Long code = 9999L;
        String orderCode = NumberUtil.getOrderCode(code);
        //1.插入数据库
        System.out.println(vo);
        Transfer transfer = new Transfer();
        transfer.setDealno(orderCode);
        transfer.setDealdate(new Date());
        transfer.setTranstype(1L);
        transfer.setTransstatus(4L);
        transfer.setAccout(vo.getAccNo());
        transfer.setAccoutname(vo.getUsername());
        transfer.setAccoutbank("中国银行");
        transfer.setAccin(vo.getToAccNo());
        transfer.setAccinname(vo.getToAccName());
        transfer.setAccinbank(vo.getToBankCode());
        transfer.setCurrtype("RMB");
        transfer.setTransfund(vo.getAccMoney());
        transfer.setKind("跨行转账");
        transfer.setDescriptions("跨行转账中");
        System.out.println(transfer);
        transferDao.insert(transfer);

        //2.掉icbc银行接口

        String secretKey=null;
        String strVo = JSON.toJSONString(vo);
        try {
            secretKey = RSAUtils.encrypt(strVo,RSAUtils.getPublicKey(publicKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = bankICBCFeign.send(secretKey);
        if(result.equals("NET_ERR")){
            transfer.setTransstatus(6L);
            transfer.setDescriptions("网络问题.导致跨行转账失败");
            transferDao.updateByPrimaryKey(transfer);

        }else if(result.equals("NOT_FIND_ACC")){
            transfer.setDescriptions("对方银行没有该账户");
            transfer.setTransstatus(5L);
            transferDao.updateByPrimaryKey(transfer);
        }else{
            System.out.println("1111111111111111111111111111111-----------------------");
            transfer.setDescriptions("跨行转账成功");
            transfer.setTransstatus(0L);
            transferDao.updateByPrimaryKey(transfer);
        }
        return result;
    }

    public String retryAcc(TransferInfoVO vo) {
        Transfer transfer = new Transfer();
        transfer.setDealno(vo.getAccNo());
        String secretKey=null;
        String strVo = JSON.toJSONString(vo);
        try {
            secretKey = RSAUtils.encrypt(strVo,RSAUtils.getPublicKey(publicKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = bankICBCFeign.send(secretKey);
        if (result.equals("NET_ERR")){
            transfer.setTransstatus(6L);
            transfer.setDescriptions("网络问题.导致跨行转账失败");
            transferDao.updateByPrimaryKeySelective(transfer);
        }else if(result.equals("NOT_FIND_ACC")){
            transfer.setDescriptions("对方银行没有该账户");
            transfer.setTransstatus(5L);
            transferDao.updateByPrimaryKeySelective(transfer);
        }else {
            transfer.setDescriptions("重新发送成功");
            transfer.setTransstatus(0L);
            transferDao.updateByPrimaryKeySelective(transfer);
        }
        return result;
    }

}
